<template>
  <div class="channel-edit">
    <van-cell>
      <div slot="title" class="title-text">我的頻道</div>
      <van-button
        class="edit-btn"
        type="danger"
        size="mini"
        plain
        round
        @click="isEdit = !isEdit"
        >{{ isEdit ? "完成" : "编辑" }}</van-button
      >
    </van-cell>
    <van-grid :gutter="10" class="my-grid">
      <van-grid-item
        class="grid-item"
        v-for="(item, index) in myChannels"
        :key="item.id"
        @click="onMyChannelClick(index)"
      >
      <van-icon slot="icon" name="clear" v-show="isEdit && !fixedChannelIsIds.includes(item.id)"/>
        <span slot="text" class="text" :class="{ active: active === index }">{{
          item.name
        }}</span>
      </van-grid-item>
    </van-grid>
    <van-cell>
      <div slot="title" class="title-text">頻道推荐</div>
    </van-cell>
    <van-grid :gutter="10" class="recommend-grid">
      <van-grid-item
        class="grid-item"
        v-for="item in rememendChannels"
        :key="item.id"
        icon="plus"
        :text="item.name"
        @click="onAddMyChannels(item)"
      />
    </van-grid>
  </div>
</template>

<script>
import { addUserChannel, delUserChannel, getAllChannels } from '@/api/channel'
import { mapState } from 'vuex'
import { setItem } from '@/utils/storage'
export default {
  name: 'ChannelEdit',
  props: {
    myChannels: {
      type: Array,
      required: true
    },
    active: {
      type: Number,
      required: true
    }
  },
  components: {},
  data () {
    return {
      allChannels: [],
      isEdit: false, // 编辑状态
      fixedChannelIsIds: [0]
    }
  },
  async created () {
    const { data: { data: { channels } } } = await getAllChannels()
    this.allChannels = channels
  },
  mounted () {},
  methods: {
    async onAddMyChannels (item) {
      this.myChannels.push(item)
      // 判断用户是否登录
      if (this.user) {
        // 已经登录,发请求
        await addUserChannel({
          id: item.id,
          seq: this.myChannels.length
        })
      } else {
        // 未登录,存储到localstorage
        setItem('TOUTIAO_CHANELS', this.myChannels)
      }
    },
    async onMyChannelClick (index) {
      if (this.isEdit) {
        // 固定频道不能被删除
        const f = this.myChannels[index]
        // console.log(this.myChannels)
        // console.log(f)
        if (this.fixedChannelIsIds.includes(f.id)) return
        //   删除
        this.myChannels.splice(index, 1)
        // 解决激活项
        if (index <= this.active) {
          this.$emit('update-active', this.active - 1, true)
        }

        // 判断用户是否登录
        if (this.user) {
        // 已经登录,发请求
          await delUserChannel(f.id)
        } else {
        // 未登录,存储到localstorage
          setItem('TOUTIAO_CHANELS', this.myChannels)
        }
      } else {
        //   切换
        this.$emit('update-active', index)
      }
    }
  },
  watch: {},
  computed: {
    ...mapState(['user']),
    rememendChannels () {
      return this.allChannels.filter(t => !(this.myChannels.find(m => m.id === t.id)))
    }
  }
}
</script>

<style lang="less" scoped>
.channel-edit {
  padding-top: 70px;
}
.title-text {
  font-size: 32px;
  color: #333333;
}
.edit-btn {
  width: 104px;
  height: 48px;
  font-size: 26px;
  color: #f85959;
  border: 1px solid #f85959;
}
/deep/ .my-grid {
  .van-icon-clear {
    position: absolute;
    right: -10px;
    top: -10px;
    font-size: 30px;
    color: #cacaca;
    z-index: 2;
  }
  .van-grid-item__icon-wrapper {
  position: unset !important;
}
}
/deep/ .grid-item {
  width: 160px;
  height: 86px;
  .van-grid-item__content {
    white-space: nowrap;
    background-color: #f4f5f6;
  }
  .van-grid-item__text,
  .text {
    font-size: 28px;
    color: #222;
    margin-top: 0;
  }
  .active {
    color: red;
  }
}
/deep/ .recommend-grid {
  .van-grid-item__content {
    flex-direction: row;
  }
  .van-icon-plus {
    font-size: 28px;
    margin-right: 6px;
  }
}
</style>
